package com.base.cn.platform.os.controller.op.keyword;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.keyword.OpKeyword;
import com.base.cn.platform.os.service.op.keyword.OpKeywordBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 敏感词Controller
 * @author zsw
 * @create 2018/4/19
 */
@RestController
@RequestMapping("/manage/op")
public class OpKeywordController extends BaseController {

    @Autowired
    private OpKeywordBiz opKeywordBiz;

    /**
     * 获取所有的敏感词
     * @return 数据
     */
    @RequestMapping("/findAllKeyword")
    public String findAllKeyword(){
        return opKeywordBiz.findAllKeyword();
    }

    /**
     * 保存敏感词
     * @param keyword 敏感词实体
     * @return
     */
    @RequestMapping("/saveKeyword")
    public Map<String, Object> saveKeyword(@RequestBody OpKeyword keyword) {
        if (ObjectUtils.isNotEmpty(keyword)) {
            if (StringUtils.isEmpty(keyword.getWord()) || StringUtils.isEmpty(keyword.getWord().trim())) {
                return ResultUtil.ERROR("敏感词不能为空");
            }
            opKeywordBiz.saveKeyword(keyword);
            return ResultUtil.SUCCESS("保存成功");
        }
        return ResultUtil.ERROR("敏感词保存失败");
    }

    /**
     * 查询敏感词列表
     *
     * @param currentPage 当前页
     * @param keyword 敏感词实体
     * @param sysUserData 是否查询创建人数据
     * @return
     */
    @RequestMapping("/queryKeywordList")
    public PageInfo<OpKeyword> queryKeywordList(@RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                                @RequestBody OpKeyword keyword,
                                                @RequestParam(value = "sysUserData",required = false,defaultValue = "false") boolean sysUserData) {
        Pagination keywordPage = new Pagination();
        keywordPage.setCurrentPage(currentPage);
        PageInfo<OpKeyword> keywordPageInfo = opKeywordBiz.queryKeywordList(keywordPage, keyword,sysUserData);
        return keywordPageInfo;
    }

    /**
     * 查询敏感词数量
     * @param keyword 敏感词实体
     * @return
     */
    @RequestMapping("/keywordCount")
    public BigDecimal count(@RequestBody OpKeyword keyword){
        return opKeywordBiz.count(keyword);
    }

    /**
     * 删除敏感词
     * @param keywordIds 敏感词id串
     * @return
     */
    @RequestMapping("/deleteKeyword")
    public Map<String, Object> deleteKeyword(@RequestParam("keywordIds") String keywordIds){
        if (ObjectUtils.isNotEmpty(keywordIds)){
            opKeywordBiz.deleteKeyword(keywordIds);
            return ResultUtil.SUCCESS("删除成功");
        }
        return ResultUtil.ERROR("删除失败");
    }
}
